//
//  SDHSportsMatchInfoCell.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/4.
//

#import "SDHSportsMatchInfoCell.h"
#import "SDHSportsMatchSoccerModel.h"
#import "SDHSportsMatchBasketModel.h"
#import "UIImageView+YQAdd.h"

@interface SDHSportsMatchInfoCell ()

@property (nonatomic, strong) UILabel *leftNameLb;
@property (nonatomic, strong) UIImageView *leftAvatarImg;
@property (nonatomic, strong) UILabel *leftScoreLb;

@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UILabel *matchStateLb;

@property (nonatomic, strong) UILabel *rightNameLb;
@property (nonatomic, strong) UIImageView *rightAvatarImg;
@property (nonatomic, strong) UILabel *rightScoreLb;

@property (nonatomic, strong) UIImageView *imgVs;


@end

@implementation SDHSportsMatchInfoCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    
    return self;
}

- (void)initUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // left
    UIImageView *imgView = [UIImageView new];
    imgView.image = [UIImage imageNamed:@"touxiang"];
    imgView.userInteractionEnabled = YES;
    [self.contentView addSubview:imgView];
    self.leftAvatarImg = imgView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [imgView addGestureRecognizer:tap];

    
    UILabel *lbName = [UILabel new];
    lbName.textAlignment = NSTextAlignmentCenter;
//    lbName.backgroundColor = [UIColor greenColor];
    lbName.font = [UIFont systemFontOfSize:15];
    lbName.numberOfLines = 0;
    [self.contentView addSubview:lbName];
    self.leftNameLb = lbName;
    
    UILabel *lbScore = [UILabel new];
    lbScore.textAlignment = NSTextAlignmentCenter;
//    lbScore.backgroundColor = [UIColor grayColor];
    lbScore.font = [UIFont boldSystemFontOfSize:22];
    lbScore.textColor = UIColorFromRGB(182, 182, 182);
    lbScore.numberOfLines = 0;
    lbScore.text = @"111";
    [self.contentView addSubview:lbScore];
    self.leftScoreLb = lbScore;
    
    // center
    UILabel *lbTime = [UILabel new];
    lbTime.textAlignment = NSTextAlignmentCenter;
//    lbTime.backgroundColor = [UIColor blueColor];
    lbTime.font = [UIFont boldSystemFontOfSize:22];
    lbTime.textColor = [UIColor blackColor];
    lbTime.numberOfLines = 1;
    lbTime.text = @"14:30";
    [self.contentView addSubview:lbTime];
    self.timeLb = lbTime;
    
    UILabel *lbState = [UILabel new];
    lbState.textAlignment = NSTextAlignmentCenter;
    lbState.backgroundColor = [UIColor grayColor];
    lbState.font = [UIFont systemFontOfSize:15];
    lbState.textColor = [UIColor whiteColor];
    lbState.numberOfLines = 1;
    lbState.text = @" has not started ";
    lbState.clipsToBounds = YES;
    lbState.layer.cornerRadius = 7;
    [self.contentView addSubview:lbState];
    self.matchStateLb = lbState;
    
    imgView = [UIImageView new];
    imgView.image = [UIImage imageNamed:@"vs"];
    [self.contentView addSubview:imgView];
    self.imgVs = imgView;
    
    // right
    imgView = [UIImageView new];
    imgView.image = [UIImage imageNamed:@"touxiang"];
    imgView.userInteractionEnabled = YES;
    [self.contentView addSubview:imgView];
    self.rightAvatarImg = imgView;
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [imgView addGestureRecognizer:tap];
    
    lbName = [UILabel new];
    lbName.textAlignment = NSTextAlignmentCenter;
//    lbName.backgroundColor = [UIColor greenColor];
    lbName.font = [UIFont systemFontOfSize:15];
    lbName.numberOfLines = 0;
    [self.contentView addSubview:lbName];
    self.rightNameLb = lbName;
    
    lbScore = [UILabel new];
    lbScore.textAlignment = NSTextAlignmentCenter;
//    lbScore.backgroundColor = [UIColor grayColor];
    lbScore.font = [UIFont boldSystemFontOfSize:22];
    lbScore.textColor = UIColorFromRGB(182, 182, 182);
    lbScore.numberOfLines = 0;
    lbScore.text = @"111";
    [self.contentView addSubview:lbScore];
    self.rightScoreLb = lbScore;
    
    __weak typeof(self) weakSelf = self;
    UIView *superview = self.contentView;
    [self.leftAvatarImg mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(superview).offset(30);
        make.top.equalTo(superview).offset(10);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
    }];
    
    [self.leftNameLb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.leftAvatarImg);
        make.top.equalTo(weakSelf.leftAvatarImg.mas_bottom).offset(8);
        make.left.equalTo(weakSelf.leftAvatarImg).offset(-15);
        make.right.equalTo(weakSelf.leftAvatarImg).offset(15);
        make.bottom.equalTo(weakSelf.contentView).offset(-5);
    }];
    
    [self.leftScoreLb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.leftNameLb.mas_right).offset(10);
        make.centerY.equalTo(weakSelf.leftAvatarImg);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
    }];
    
    [self.timeLb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.timeLb.superview);
        make.top.equalTo(weakSelf.leftScoreLb);
        make.width.equalTo(@80);
        make.height.equalTo(@30);
    }];
    
    [self.matchStateLb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.matchStateLb.superview);
        make.top.equalTo(weakSelf.leftNameLb.mas_top);
    }];
    
    [self.imgVs mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.timeLb);
        make.centerY.equalTo(weakSelf.contentView);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
    }];
    
    // right
    [self.rightAvatarImg mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(superview).offset(-30);
        make.top.equalTo(superview).offset(10);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
    }];
    
    [self.rightNameLb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.rightAvatarImg);
        make.top.equalTo(self.rightAvatarImg.mas_bottom).offset(8);
        make.left.equalTo(weakSelf.rightAvatarImg).offset(-15);
        make.right.equalTo(weakSelf.rightAvatarImg).offset(15);
        make.bottom.equalTo(weakSelf.contentView).offset(-5);
    }];
    
    [self.rightScoreLb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rightNameLb.mas_left).offset(-10);
        make.centerY.equalTo(self.rightAvatarImg);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
    }];
}

- (void)updateContent:(id)object {
    self.model = object;
    if ([object isKindOfClass:[SDHSportsMatchSoccerModel class]]) {
        SDHSportsMatchSoccerModel *model = (SDHSportsMatchSoccerModel *)object;
        self.leftNameLb.text = model.guestTeamEnName;
        self.rightNameLb.text = model.hostTeamEnName;
        UIImage *placeholder = [UIImage imageNamed:@"touxiang"];
        
        [self.leftAvatarImg setImageWithURL:[NSURL URLWithString:model.guestTeamLogo] placeholderImage:placeholder];
        [self.rightAvatarImg setImageWithURL:[NSURL URLWithString:model.hostTeamLogo] placeholderImage:placeholder];
        
        NSArray *scoreArr = [model.gameScore componentsSeparatedByString:@":"];
        if (scoreArr.count >= 2) {
            self.leftScoreLb.text = [scoreArr firstObject];
            self.rightScoreLb.text = [scoreArr lastObject];
        }
        
        NSDate *tmpdate = [NSDate dateFromString:model.curMatchTime withFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"HH:mm"];
        NSString *timeStr = [formatter stringFromDate:tmpdate];
        if (timeStr.length > 0) {
            self.timeLb.text = timeStr;
        }
        
        // 1: ongoing, 2: coming, 3: finished, 4: to be confirmed
        UIColor *stateBgcolor = [UIColor blackColor];
        NSString *state = @" coming ";
        switch (model.state) {
            case 1:
                state = @" on going ";
                stateBgcolor = UIColorFromRGB(229, 86, 133);
                break;
            case 2:
                state = @" coming ";
                stateBgcolor = UIColorFromRGB(149, 195, 235);
                break;
            case 3:
                state = @" finished ";
                stateBgcolor = UIColorFromRGB(234, 234, 234);
                break;
            case 4:
                state = @" to be confirmed ";
                break;
            case 5:
                break;
            default:
                break;
        }

        self.matchStateLb.text = state;
        self.matchStateLb.backgroundColor = stateBgcolor;
        
        self.imgVs.hidden = YES;
    } else if ([object isKindOfClass:[SDHSportsMatchBasketModel class]]) {
        SDHSportsMatchBasketModel *model = (SDHSportsMatchBasketModel *)object;
        self.leftNameLb.text = model.guest_en_name;
        self.rightNameLb.text = model.host_en_name;
        UIImage *placeholder = [UIImage imageNamed:@"touxiang"];
        
        [self.leftAvatarImg setImageWithURL:[NSURL URLWithString:model.guest_img] placeholderImage:placeholder];
        [self.rightAvatarImg setImageWithURL:[NSURL URLWithString:model.host_img] placeholderImage:placeholder];
        
        self.leftScoreLb.text = model.guest_score;
        self.rightScoreLb.text = model.host_score;
        
        self.timeLb.text = @"";
        
        // 1: ongoing, 2: coming, 3: finished, 4: to be confirmed
        UIColor *stateBgcolor = [UIColor blackColor];
        NSString *state = @"";
        
        self.matchStateLb.text = state;
        self.matchStateLb.backgroundColor = stateBgcolor;
        
        self.matchStateLb.hidden = YES;
        self.timeLb.hidden = YES;
        self.imgVs.hidden = NO;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)avatarClick:(UIGestureRecognizer *)gesture {
    if (self.leftAvatarImg == gesture.view) {
        if (self.leftClick) {
            self.leftClick(_model);
        }
    } else {
        if (self.rightClick) {
            self.rightClick(_model);
        }
    }
}

@end
